import {AfterViewInit, ChangeDetectorRef, Component, Inject, Optional} from '@angular/core';
import {GnForm, GnFormField, GnObjectUtils, GnValidators} from "geminis-ng";
import {MAT_DIALOG_DATA} from "@angular/material";
import {MatDialogRef} from "@angular/material/dialog";
import uuid from 'uuid';
import {InputTypes} from "../InputTypes";

@Component({
  selector: 'gn-workflow-manager-model-form-property',
  templateUrl: './property.component.html'
})
export class PropertyComponent extends GnForm implements AfterViewInit {

  fields: GnFormField[] = [
    {
      label: "显示名称",
      field: "label",
      type: "input",
      validators: [{name: 'required', message: '请输入显示名称', validatorFn: GnValidators.instance.required}]
    },
    {
      label: "数据项",
      field: "field",
      type: "input",
      validators: [{name: 'required', message: '请输入数据项', validatorFn: GnValidators.instance.required}]
    },
    {
      label: "输入类型",
      field: "type",
      type: "select",
      defaultValue: "input",
      options: {
        options: InputTypes
      }
    },
    {
      label: "显示顺序",
      field: "showOrder",
      type: "input",
      options: {type: 'number'},
      validators: [{name: 'required', message: '请输入显示顺序', validatorFn: GnValidators.instance.required}]
    },
    {
      label: "跨行",
      field: "rowspan",
      type: "input",
      options: {type: 'number'},
      validators: [{name: 'required', message: '请输入跨行', validatorFn: GnValidators.instance.required}]
    },
    {
      label: "跨列",
      field: "colspan",
      type: "input",
      options: {type: 'number'},
      validators: [{name: 'required', message: '请输入跨列', validatorFn: GnValidators.instance.required}]
    }
  ];

  constructor(@Optional() @Inject(MAT_DIALOG_DATA) private readonly property: any,
              private cdr: ChangeDetectorRef,
              private dialogRef: MatDialogRef<any>) {
    super();

    if (!this.property) {
      this.property = {
        id: uuid.v4(),
        showOrder: 0,
        colspan: 1,
        rowspan: 1,
        label: '新建项',
        field: 'newItem',
        type: 'input',
        validators: []
      };
    }
  }

  ngAfterViewInit(): void {
    this.formComponent.form.setValue(this.property);
    this.cdr.detectChanges();
  }

  save() {
    GnObjectUtils.copy(this.formComponent.form.fullValue, this.property);
    this.dialogRef.close(this.property);
  }


}
